﻿<UserControl x:Class="Adan.Client.Common.Controls.ActionEditorControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:Controls="clr-namespace:Adan.Client.Common.Controls" 
             xmlns:Utils="clr-namespace:Adan.Client.Common.Utils"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Controls:GenericTemplateSelector x:Key="actionTemplateSelector" />
        <Utils:TextColorToBrushConverter x:Key="TextColorToBrushConverter" />
        <Utils:BackgroundTextColorToBrushConverter x:Key="BackgroundTextColorToBrushConverter" />
        <Utils:InverseBooleanConverter x:Key="InverseBooleanConverter" />
        <DataTemplate x:Key="SendTextActionViewModelTemplate" DataType="SendTextActionViewModel">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Grid.Row="0">Command:</Label>
                <TextBox Grid.Row="1" Text="{Binding Path=CommandText, UpdateSourceTrigger=PropertyChanged}" />
                <Controls:ActionParametersEditorControl Grid.Row="2" DataContext="{Binding Path=ParametersModel}" />
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="OutputToMainWindowActionViewModelTemplate" DataType="OutputToMainWindowActionViewModel" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Grid.Row="0">Text to output:</Label>
                <TextBox Grid.Row="1" Text="{Binding Path=TextToOutput, UpdateSourceTrigger=PropertyChanged}" />
                <Grid Grid.Row="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Label Grid.Column="0" Grid.Row="0" VerticalAlignment="Center">Text color:</Label>
                    <Controls:ColorSelector Grid.Column="1"  Grid.Row="0" Margin="5,5,0,0" SelectedColor="{Binding Path=TextColor, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  Width="150"/>
                    <Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center">Background color:</Label>
                    <Controls:BackgroundColorSelector Grid.Column="1" Grid.Row="1" Margin="5,5,0,0"  SelectedColor="{Binding Path=BackgroundColor, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="150"/>
                    <TextBlock Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" FontSize="22" Text="Sample text" VerticalAlignment="Center" HorizontalAlignment="Center" Padding="15,0,15,0" FontFamily="../Resources/consola.ttf#Consolas" Foreground="{Binding Path=TextColor, Converter={StaticResource TextColorToBrushConverter}}" Background="{Binding Path=BackgroundColor, Converter={StaticResource BackgroundTextColorToBrushConverter}}" />
                </Grid>
                <Controls:ActionParametersEditorControl Grid.Row="3" DataContext="{Binding Path=ParametersModel}" />
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="ConditionalActionViewModelTemplate" DataType="ConditionalActionViewModel">
            <StackPanel Margin="0,5,0,0">
                <Border Margin="10,2,2,2" Padding="2" BorderBrush="Black" BorderThickness="1"  CornerRadius="2,2,2,2">
                    <StackPanel Orientation="Vertical">
                        <Label VerticalAlignment="Center">Left operand:</Label>
                        <ComboBox Width="200" HorizontalAlignment="Left" SelectedValue="{Binding Path=LeftConditionParameterDescriptor}" ItemsSource="{Binding ParameterDescriptions}" DisplayMemberPath="DisplayName"/>
                        <ContentControl Content="{Binding Path=LeftConditionParameter}" Grid.Column="1" Margin="2,0,2,2" ContentTemplateSelector="{DynamicResource GenericTemplateSelector}"/>
                    </StackPanel>
                </Border>
                <ComboBox Width="150" HorizontalAlignment="Left" SelectedValue="{Binding Path=Condition}" ItemsSource="{StaticResource ActionConditionTypes}" Margin="0,5,0,5"/>
                <Border Margin="10,2,2,2" Padding="2" BorderBrush="Black" BorderThickness="1"  CornerRadius="2,2,2,2" Visibility="{Binding Path=IsBinaryOperator, Converter={StaticResource BooleanToVisibilityConverter}}">
                    <StackPanel Orientation="Vertical">
                        <Label VerticalAlignment="Center">Right operand:</Label>
                        <ComboBox Width="200" HorizontalAlignment="Left" SelectedValue="{Binding Path=RightConditionParameterDescriptor}" ItemsSource="{Binding ParameterDescriptions}" DisplayMemberPath="DisplayName"/>
                        <ContentControl Content="{Binding Path=RightConditionParameter}" Margin="2,0,2,2" ContentTemplateSelector="{DynamicResource GenericTemplateSelector}"/>
                    </StackPanel>
                </Border>
                <TextBlock Text="Actions to execute if condition is true:" />
                <Controls:ActionEditorControl DataContext="{Binding Path=ActionsToExecute}" VerticalAlignment="Stretch"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="SetVariableValueActionViewModelTemplate" DataType="SetVariableValueActionViewModel">
            <StackPanel Margin="0,5,0,0">
                <Label>Variable name:</Label>
                <ComboBox Text="{Binding Path=VariableName, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=AllVariables}"
                          DisplayMemberPath="Name" IsEditable="True" Width="200" HorizontalAlignment="Left" />
                <CheckBox Content="Silently set variable value." IsChecked="{Binding Path=SilentSet, Mode=TwoWay}"
                          VerticalAlignment="Center" Grid.Column="0"/>
                <Label>Value:</Label>
                <ComboBox Width="200" HorizontalAlignment="Left" SelectedValue="{Binding Path=ValueToSetParameterDescriptor}"
                          ItemsSource="{Binding ParameterDescriptions}" DisplayMemberPath="DisplayName"/>
                <ContentControl Content="{Binding Path=ValueToSet}" Margin="2,0,2,2"
                                ContentTemplateSelector="{DynamicResource GenericTemplateSelector}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ClearVariableValueActionViewModelTemplate" DataType="ClearVariableValueActionViewModel">
            <StackPanel Margin="0,5,0,0">
                <Label>Variable name:</Label>
                <ComboBox Text="{Binding Path=VariableName, UpdateSourceTrigger=PropertyChanged}"
                          ItemsSource="{Binding Path=AllVariables}" DisplayMemberPath="Name" IsEditable="True"
                          Width="200"  HorizontalAlignment="Left"  Margin="0,0,0,5"/>
                <CheckBox Content="Silently set variable value." IsChecked="{Binding Path=SilentSet, Mode=TwoWay}"
                          VerticalAlignment="Center" Grid.Column="0"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="StartLogActionViewModelTemplate" DataType="StartLogActionViewModel">
            <StackPanel Margin="0,5,0,0">
                <Label>Log name:</Label>
                <ComboBox Width="200" HorizontalAlignment="Left" SelectedValue="{Binding Path=LogNameParameterDescriptor}" ItemsSource="{Binding ParameterDescriptions}" DisplayMemberPath="DisplayName"/>
                <ContentControl Content="{Binding Path=LogNameParameter}" Margin="2,0,2,2" ContentTemplateSelector="{DynamicResource GenericTemplateSelector}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="StopLogActionViewModelTemplate" DataType="StopLogActionViewModel">
        </DataTemplate>
        <DataTemplate x:Key="EnableGroupActionViewModelTemplate" DataType="EnableGroupActionViewModel">
            <StackPanel>
                <Label>Group to enable:</Label>
                <ComboBox Width="200" Text="{Binding Path=GroupNameToEnable, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=AllGroups}" DisplayMemberPath="Name" IsEditable="True" HorizontalAlignment="Left" Margin="0,0,0,5" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="DisableGroupActionViewModelTemplate" DataType="DisableGroupActionViewModel">
            <StackPanel>
                <Label>Group to disable:</Label>
                <ComboBox Width="200"  Text="{Binding Path=GroupNameToDisable,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=AllGroups}" DisplayMemberPath="Name" IsEditable="True" HorizontalAlignment="Left" Margin="0,0,0,5" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ShowOutputWindowActionViewModelTemplate" DataType="ShowOutputWindowActionViewModel">
            <StackPanel>
                <Label>Window name to switch to (if left empty will switch to next window in a cycle):</Label>
                <ComboBox Width="200"  Text="{Binding Path=OutputWindowName,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=AllGroups}" DisplayMemberPath="Name" IsEditable="True" HorizontalAlignment="Left" Margin="0,0,0,5" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="SendToWindowActionViewModelTemplate" DataType="SendToWindowActionViewModel">
            <StackPanel>
                <Label>Window name to send commands to:</Label>
                <ComboBox Width="200"  Text="{Binding Path=OutputWindowName,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding Path=AllGroups}" DisplayMemberPath="Name" IsEditable="True" HorizontalAlignment="Left" Margin="0,0,0,5" IsEnabled="{Binding Path=SendToAllWindows, Converter={StaticResource InverseBooleanConverter}}" />
                <CheckBox Content="Send to all windows." IsChecked="{Binding Path=SendToAllWindows, Mode=TwoWay}"/>
                <TextBlock Text="Actions to execute if condition is true:" />
                <Controls:ActionEditorControl DataContext="{Binding Path=ActionsToExecute}" VerticalAlignment="Stretch" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ToggleFullScreenModeActionViewModelTemplate" DataType="ToggleFullScreenModeActionViewModel">
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ItemsControl ItemsSource="{Binding Path=Actions}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <ItemsControl.Template>
                <ControlTemplate>
                    <StackPanel>
                        <ItemsPresenter />
                    </StackPanel>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Expander IsExpanded="True" HorizontalAlignment="Stretch" Visibility="Visible" Header="{Binding}">
                        <Expander.HeaderTemplate>
                            <DataTemplate>
                                <Grid Margin="0,0,5,0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding Path=ActionDescription}" Grid.Column="0" TextTrimming="CharacterEllipsis" />
                                    <ComboBox Grid.Column="1" Width="150" SelectedValue="{Binding Path=ActionDescriptor}" ItemsSource="{Binding AllActionDescriptions}" DisplayMemberPath="DisplayName"/>
                                    <Button Grid.Column="2" Width="22" Height="22" Command="{Binding Path=DataContext.AddActionCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}}" CommandParameter="{Binding}">
                                        <ContentControl Style="{StaticResource AddItemIcon}" Width="14" Height="14"/>
                                    </Button>
                                    <Button Grid.Column="3" Width="22" Height="22" Command="{Binding Path=DataContext.RemoveActionCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}}" CommandParameter="{Binding}">
                                        <ContentControl Style="{StaticResource DeleteIcon}" Width="14" Height="14"/>
                                    </Button>
                                    <Button Grid.Column="4" Width="22" Height="22" Command="{Binding Path=DataContext.MoveActionUpCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}}" CommandParameter="{Binding}">
                                        <ContentControl Style="{StaticResource UpIcon}" Width="14" Height="14"/>
                                    </Button>
                                    <Button Grid.Column="5" Width="22" Height="22" Command="{Binding Path=DataContext.MoveActionDownCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}}" CommandParameter="{Binding}">
                                        <ContentControl Style="{StaticResource DownIcon}" Width="14" Height="14"/>
                                    </Button>
                                </Grid>
                            </DataTemplate>
                        </Expander.HeaderTemplate>
                        <ContentPresenter Content="{Binding}" ContentTemplateSelector="{Binding Source={StaticResource actionTemplateSelector}}" Margin="15,0,5,0" />
                    </Expander>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</UserControl>
